package com.example.exam1.dao;

import com.example.exam1.entity.Book;
import com.example.exam1.util.JDBCUtils;

import java.sql.*;

/**
 * @author wushuang
 * recorrected by Deepwind
 */

public class BookDao {
    private Connection conn = null;
    private PreparedStatement ps = null;
    private ResultSet rs = null;

    /**
     * 添加图书
     * @param book Book
     * @return 状态码
     */
    public int addBook(Book book) {
        int count = 0;
        try {
            conn = JDBCUtils.getConn();

            String sql = "insert into tb_book (name,author,number) value (?,?,?)";
            ps = conn.prepareStatement(sql);
            ps.setString(1, book.getName());
            ps.setString(2, book.getAuthor());
            ps.setString(3, book.getNumber());
            count = ps.executeUpdate();
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.release(rs, ps, conn);
        }
        return count;
    }

    /**
     * 删除图书
     * @param book Book
     * @return 状态码
     */
    public int delBook(Book book) {
        int count = 0;
        try {
            conn = JDBCUtils.getConn();

            String sql = "delete from tb_book where number = ?";
            ps = conn.prepareStatement(sql);
            ps.setString(1, book.getNumber());
            count = ps.executeUpdate();
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.release(rs, ps, conn);
        }
        return count;
    }

    /**
     * 检索图书
     * @param book Book
     * @return 检索到的图书对象
     */
    public Book searchBook(Book book) {
        Book newBook = null;
        try {
            conn = JDBCUtils.getConn();

            String sql = "select * from tb_book where number = ?";
            ps = conn.prepareStatement(sql);
            ps.setString(1, book.getNumber());
            rs = ps.executeQuery();

            while (rs.next()) {
                Book temp = new Book();
                temp.setId(rs.getInt("id"));
                temp.setName(rs.getString("name"));
                temp.setAuthor(rs.getString("author"));
                temp.setNumber(rs.getString("number"));
                // 此处理应返回列表，但为了简化题目，改为返回最后一本书
                newBook = temp;
            }
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.release(rs, ps, conn);
        }
        return newBook;
    }

    /**
     * 更新图书
     * @param book Book
     * @return 状态码
     */
    public int updateBook(Book book) {
        int count = 0;
        try {
            conn = JDBCUtils.getConn();

            String sql = "update tb_book set name = ?,author = ? where number = ?";
            ps = conn.prepareStatement(sql);
            ps.setString(1, book.getName());
            ps.setString(2, book.getAuthor());
            ps.setString(3, book.getNumber());
            count = ps.executeUpdate();
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.release(rs, ps, conn);
        }
        return count;
    }

    /**
     * 判断图书是否存在
     * @param book Book
     * @return Boolean
     */
    public Boolean isBookExisted(Book book){
        return searchBook(book) != null;
    }
}
